package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.exception.BaseException;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 衷岚
 * @version 1.0
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        //获取日期列表
        List<LocalDate> listOfDate = getDateList(begin, end);
        String dateList = StringUtils.join(listOfDate, ",");

        //用于存放对应日期的营业额
        List<Double> listOfTurnover = new ArrayList<>();


        //获取日期列表对应的营业额
        for (LocalDate date :listOfDate) {

            //获取当天最大最小时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            HashMap<String, Object> map = new HashMap<>();
            map.put("beginTime", beginTime);
            map.put("endTime", endTime);
            map.put("status", Orders.COMPLETED);

            Double turnover = orderMapper.sumByMap(map);
            //判断当天营业额 为空加0
            turnover = (turnover==null)? 0.0 : turnover;
            listOfTurnover.add(turnover);
//            if (turnover == null) {
//                listOfTurnover.add(0.0);
//            } else {
//                listOfTurnover.add(turnover);
//            }

        }
        String turnoverList = StringUtils.join(listOfTurnover, ",");

        return new TurnoverReportVO(dateList, turnoverList);

    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        //获取日期列表
        List<LocalDate> listOfDate = getDateList(begin, end);
        String dateList = StringUtils.join(listOfDate, ",");

        //用于存放至今所有用户数量
        List<Integer> listOfTotalUser = new ArrayList<>();
        //用于存放当天新增用户数量
        List<Integer> listOfNewUser = new ArrayList<>();


        //获取日期列表对应用户数量
        for (LocalDate date :listOfDate) {

            //获取当天最大最小时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            HashMap<String, Object> map = new HashMap<>();

            //查询至今总用户数量
            map.put("endTime", endTime);
            Integer totalUser = userMapper.sumByMap(map);
            totalUser = (totalUser==null)? 0: totalUser;
            listOfTotalUser.add(totalUser);

            //查询当天新增用户数量
            map.put("beginTime", beginTime);
            Integer newUser = userMapper.sumByMap(map);
            newUser = (newUser==null)? 0: newUser;
            listOfNewUser.add(newUser);

        }
        String totalUserList = StringUtils.join(listOfTotalUser, ",");
        String newUserList = StringUtils.join(listOfNewUser, ",");


        return new UserReportVO(dateList,totalUserList,newUserList);

    }

    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        //获取日期列表
        List<LocalDate> listOfDate = getDateList(begin, end);
        String dateList = StringUtils.join(listOfDate, ",");

        //存放当天订单总数
        List<Integer> listOfTotalOrder = new ArrayList<>();

        //存放当天有效订单数 --已完成订单数
        List<Integer> listOfValidOrder = new ArrayList<>();

        //存放所有订单总数
        Integer totalOrderCount = 0;
        //存放有效订单总数
        Integer validOrderCount = 0;

        //获取日期列表对应订单数
        for (LocalDate date : listOfDate) {
            //当天最大最小时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map<String, Object> map = new HashMap<>();
            map.put("beginTime", beginTime);
            map.put("endTime", endTime);
            //获取当天所有订单
            Integer totalOrder= orderMapper.countOrderByMap(map);
            totalOrder = (totalOrder==null)? 0: totalOrder;
            listOfTotalOrder.add(totalOrder);

            //获取当天有效订单
            map.put("status", Orders.COMPLETED);
            Integer validOrder = orderMapper.countOrderByMap(map);
            validOrder = (validOrder==null)? 0: validOrder;
            listOfValidOrder.add(validOrder);

            //统计所有订单
            totalOrderCount+= totalOrder;

            //统计有效订单
            validOrderCount+= validOrder;


        }
        //计算订单完成率
        Double orderCompletionRate = validOrderCount/(double)totalOrderCount;

        String orderCountList = StringUtils.join(listOfTotalOrder, ",");
        String validOrderCountList = StringUtils.join(listOfValidOrder, ",");

        return new OrderReportVO(dateList,orderCountList,validOrderCountList,totalOrderCount,validOrderCount,orderCompletionRate);



    }


    /**
     * 获取销量前10的商品
     * @return
     */
    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {

        //存放商品名
        List<String> listOfName = new ArrayList<>();
        //存放对应销量
        List<Integer> listOfNumber = new ArrayList<>();

        //top10

            //最大最小时间
            LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

            Map map = new HashMap();
            map.put("beginTime", beginTime);
            map.put("endTime", endTime);
            map.put("status", Orders.COMPLETED);

            //查找对应时间段内销量前10商品
            List<GoodsSalesDTO> list = orderDetailMapper.getTop10(map);

            //判断是否有商品售出
            if (list!=null && list.size()>0) {
                for (GoodsSalesDTO goods :list) {
                    listOfName.add(goods.getName());
                    listOfNumber.add(goods.getNumber());
                }
            }
        String nameList = StringUtils.join(listOfName, ",");
        String numberList = StringUtils.join(listOfNumber, ",");
        return new SalesTop10ReportVO(nameList,numberList);





    }




    /**
     * 获取日期列表
     * @param begin
     * @param end
     * @return
     */
    private List<LocalDate> getDateList(LocalDate begin,LocalDate end){
        //存储从begin到end的每天日期
        List<LocalDate> dateList = new ArrayList<>();

        dateList.add(begin);
        while(!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        return dateList;
    }
}
